// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import zombie.characters.IsoPlayer;
import zombie.core.math.PZMath;

public class SearchMode {
    private static SearchMode instance;
    private float fadeTime = 1.0F;
    private SearchMode.PlayerSearchMode[] plrModes = new SearchMode.PlayerSearchMode[4];

    public static SearchMode getInstance() {
        if (instance == null) {
            instance = new SearchMode();
        }

        return instance;
    }

    private SearchMode() {
        for (int _int = 0; _int < this.plrModes.length; _int++) {
            this.plrModes[_int] = new SearchMode.PlayerSearchMode(_int, this);
            this.plrModes[_int].blur.setTargets(1.0F, 1.0F);
            this.plrModes[_int].desat.setTargets(0.85F, 0.85F);
            this.plrModes[_int].radius.setTargets(4.0F, 4.0F);
            this.plrModes[_int].darkness.setTargets(0.0F, 0.0F);
            this.plrModes[_int].gradientWidth.setTargets(4.0F, 4.0F);
        }
    }

    public SearchMode.PlayerSearchMode getSearchModeForPlayer(int index) {
        return this.plrModes[index];
    }

    public float getFadeTime() {
        return this.fadeTime;
    }

    public void setFadeTime(float _fadeTime) {
        this.fadeTime = _fadeTime;
    }

    public boolean isOverride(int plrIdx) {
        return this.plrModes[plrIdx].override;
    }

    public void setOverride(int plrIdx, boolean enabled) {
        this.plrModes[plrIdx].override = enabled;
    }

    public SearchMode.SearchModeFloat getRadius(int plrIdx) {
        return this.plrModes[plrIdx].radius;
    }

    public SearchMode.SearchModeFloat getGradientWidth(int plrIdx) {
        return this.plrModes[plrIdx].gradientWidth;
    }

    public SearchMode.SearchModeFloat getBlur(int plrIdx) {
        return this.plrModes[plrIdx].blur;
    }

    public SearchMode.SearchModeFloat getDesat(int plrIdx) {
        return this.plrModes[plrIdx].desat;
    }

    public SearchMode.SearchModeFloat getDarkness(int plrIdx) {
        return this.plrModes[plrIdx].darkness;
    }

    public boolean isEnabled(int plrIdx) {
        return this.plrModes[plrIdx].enabled;
    }

    public void setEnabled(int plrIdx, boolean b) {
        SearchMode.PlayerSearchMode playerSearchMode = this.plrModes[plrIdx];
        if (b && !playerSearchMode.enabled) {
            playerSearchMode.enabled = true;
            this.FadeIn(plrIdx);
        } else if (!b && playerSearchMode.enabled) {
            playerSearchMode.enabled = false;
            this.FadeOut(plrIdx);
        }
    }

    private void FadeIn(int _int) {
        SearchMode.PlayerSearchMode playerSearchMode = this.plrModes[_int];
        playerSearchMode.timer = Math.max(playerSearchMode.timer, 0.0F);
        playerSearchMode.doFadeIn = true;
        playerSearchMode.doFadeOut = false;
    }

    private void FadeOut(int _int) {
        SearchMode.PlayerSearchMode playerSearchMode = this.plrModes[_int];
        playerSearchMode.timer = Math.min(playerSearchMode.timer, this.fadeTime);
        playerSearchMode.doFadeIn = false;
        playerSearchMode.doFadeOut = true;
    }

    public void update() {
        for (int _int = 0; _int < this.plrModes.length; _int++) {
            SearchMode.PlayerSearchMode playerSearchMode = this.plrModes[_int];
            playerSearchMode.update();
        }
    }

    public static void reset() {
        instance = null;
    }

    public static class PlayerSearchMode {
        private final int plrIndex;
        private final SearchMode parent;
        private boolean override = false;
        private boolean enabled = false;
        private final SearchMode.SearchModeFloat radius = new SearchMode.SearchModeFloat(0.0F, 50.0F, 1.0F);
        private final SearchMode.SearchModeFloat gradientWidth = new SearchMode.SearchModeFloat(0.0F, 20.0F, 1.0F);
        private final SearchMode.SearchModeFloat blur = new SearchMode.SearchModeFloat(0.0F, 1.0F, 0.01F);
        private final SearchMode.SearchModeFloat desat = new SearchMode.SearchModeFloat(0.0F, 1.0F, 0.01F);
        private final SearchMode.SearchModeFloat darkness = new SearchMode.SearchModeFloat(0.0F, 1.0F, 0.01F);
        private float timer;
        private boolean doFadeOut;
        private boolean doFadeIn;

        public PlayerSearchMode(int index, SearchMode sm) {
            this.plrIndex = index;
            this.parent = sm;
        }

        public boolean isShaderEnabled() {
            return this.enabled || this.doFadeIn || this.doFadeOut;
        }

        private boolean isPlayerExterior() {
            IsoPlayer isoPlayer = IsoPlayer.players[this.plrIndex];
            return isoPlayer != null && isoPlayer.getCurrentSquare() != null && !isoPlayer.getCurrentSquare().isInARoom();
        }

        public float getShaderBlur() {
            return this.isPlayerExterior() ? this.blur.getExterior() : this.blur.getInterior();
        }

        public float getShaderDesat() {
            return this.isPlayerExterior() ? this.desat.getExterior() : this.desat.getInterior();
        }

        public float getShaderRadius() {
            return this.isPlayerExterior() ? this.radius.getExterior() : this.radius.getInterior();
        }

        public float getShaderGradientWidth() {
            return this.isPlayerExterior() ? this.gradientWidth.getExterior() : this.gradientWidth.getInterior();
        }

        public float getShaderDarkness() {
            return this.isPlayerExterior() ? this.darkness.getExterior() : this.darkness.getInterior();
        }

        public SearchMode.SearchModeFloat getBlur() {
            return this.blur;
        }

        public SearchMode.SearchModeFloat getDesat() {
            return this.desat;
        }

        public SearchMode.SearchModeFloat getRadius() {
            return this.radius;
        }

        public SearchMode.SearchModeFloat getGradientWidth() {
            return this.gradientWidth;
        }

        public SearchMode.SearchModeFloat getDarkness() {
            return this.darkness;
        }

        private void update() {
            if (!this.override) {
                if (this.doFadeIn) {
                    this.timer = this.timer + zombie.GameTime.getInstance().getTimeDelta();
                    this.timer = PZMath.clamp(this.timer, 0.0F, this.parent.fadeTime);
                    float float0 = PZMath.clamp(this.timer / this.parent.fadeTime, 0.0F, 1.0F);
                    this.blur.update(float0);
                    this.desat.update(float0);
                    this.radius.update(float0);
                    this.darkness.update(float0);
                    this.gradientWidth.equalise();
                    if (this.timer >= this.parent.fadeTime) {
                        this.doFadeIn = false;
                    }
                } else if (this.doFadeOut) {
                    this.timer = this.timer - zombie.GameTime.getInstance().getTimeDelta();
                    this.timer = PZMath.clamp(this.timer, 0.0F, this.parent.fadeTime);
                    float float1 = PZMath.clamp(this.timer / this.parent.fadeTime, 0.0F, 1.0F);
                    this.blur.update(float1);
                    this.desat.update(float1);
                    this.radius.update(float1);
                    this.darkness.update(float1);
                    this.gradientWidth.equalise();
                    if (this.timer <= 0.0F) {
                        this.doFadeOut = false;
                    }
                } else {
                    if (this.enabled) {
                        this.blur.equalise();
                        this.desat.equalise();
                        this.radius.equalise();
                        this.darkness.equalise();
                        this.gradientWidth.equalise();
                    } else {
                        this.blur.reset();
                        this.desat.reset();
                        this.radius.reset();
                        this.darkness.reset();
                        this.gradientWidth.equalise();
                    }
                }
            }
        }
    }

    public static class SearchModeFloat {
        private final float min;
        private final float max;
        private final float stepsize;
        private float exterior;
        private float targetExterior;
        private float interior;
        private float targetInterior;

        private SearchModeFloat(float float0, float float1, float float2) {
            this.min = float0;
            this.max = float1;
            this.stepsize = float2;
        }

        public void set(float _exterior, float _targetExterior, float _interior, float _targetInterior) {
            this.setExterior(_exterior);
            this.setTargetExterior(_targetExterior);
            this.setInterior(_interior);
            this.setTargetInterior(_targetInterior);
        }

        public void setTargets(float _targetExterior, float _targetInterior) {
            this.setTargetExterior(_targetExterior);
            this.setTargetInterior(_targetInterior);
        }

        public float getExterior() {
            return this.exterior;
        }

        public void setExterior(float _exterior) {
            this.exterior = _exterior;
        }

        public float getTargetExterior() {
            return this.targetExterior;
        }

        public void setTargetExterior(float _targetExterior) {
            this.targetExterior = _targetExterior;
        }

        public float getInterior() {
            return this.interior;
        }

        public void setInterior(float _interior) {
            this.interior = _interior;
        }

        public float getTargetInterior() {
            return this.targetInterior;
        }

        public void setTargetInterior(float _targetInterior) {
            this.targetInterior = _targetInterior;
        }

        public void update(float delta) {
            this.exterior = delta * this.targetExterior;
            this.interior = delta * this.targetInterior;
        }

        public void equalise() {
            if (!PZMath.equal(this.exterior, this.targetExterior, 0.001F)) {
                this.exterior = PZMath.lerp(this.exterior, this.targetExterior, 0.01F);
            } else {
                this.exterior = this.targetExterior;
            }

            if (!PZMath.equal(this.interior, this.targetInterior, 0.001F)) {
                this.interior = PZMath.lerp(this.interior, this.targetInterior, 0.01F);
            } else {
                this.interior = this.targetInterior;
            }
        }

        public void reset() {
            this.exterior = 0.0F;
            this.interior = 0.0F;
        }

        public float getMin() {
            return this.min;
        }

        public float getMax() {
            return this.max;
        }

        public float getStepsize() {
            return this.stepsize;
        }
    }
}
